import random

import requests
import time
import ssl


# 登录用户的信息需要自己找到head 填写
#
import functions
ssl._create_default_https_context = ssl._create_unverified_context


latiAndlongiList = [
    [[30.2084, 117.000000], [32.117435, 120.212011]],
]


basicUrl = "https://h5.ele.me/restapi/shopping/v3/restaurants?latitude=&longitude=&keyword=&offset="  ##饿了么店铺信息url基本格式
basicLimit = "&limit=200"   ##limit参数：店铺信息Json中每一页显示的店铺条数
header = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36",
    "host": "h5.ele.me",
    "cna":"2QDdF+AaKXkCAdOiUVykGIJh",
     "_tb_token_":"e3e349da7b6e8",
    "munb": "2206664082532",
     "SID":"JAAAAOjVuxSu7AAGSQCfyZBB0yQYNsSj7bQiDhlQF_9J3vTrzCIwTbji",
    "USERID":"1000018220206"
}

# 我们数据的爬取必须要进行提前的登录
# 得到我们用户的登录信息 然后再进行注册

cookies = {
    "__wpkreporterwid_": "a373f267-9196-4ff4-a0b0-e9d92c375eec",
    "ubt_ssid": "qil1xwlzbdx4o5ho5rf1jilr7jvfv5ye_2020-10-20",
    "perf_ssid": "qw5s428ug4kdhlhf7mm2emfftr43snc5_2020-10-20",
    "ut_ubt_ssid": "itpc1g9p8oh0y1opq119w4jggl9ifcvr_2020-10-20",
    "_bl_uid": "6ekstge1h74x4O4X7wztdpwsIz0F",
    "xlly_s": "1",
    "cna": "2QDdF+AaKXkCAdOiUVykGIJh",
    "_utrace": "cfff368886e692d1ef6d2130ba705782_2020-10-20",
    "_samesite_flag_": "true",
    "cookie2": "1565c212dc8eb749427ef10a4ce551fe",
    "t": "56c7764336ebf62b767217a9a79f2341",
    "_tb_token_": "e4bbe7e0e5538",
    "csg": "8bbe7af0",
    "t_eleuc4": "id4=0%40BA%2FvuHCrrRtTGeE%2B0d%2F2th3XV2OqQxSMAN2dYA%3D%3D",
    "munb": "2206664082532",
    "SID": "CQAAAOjVuxSu7AAGSQDQp_63keSowBBDfyKFG9It0ru4vgSyzKvyNEHB",
    "USERID": "1000018220206",
    "UTUSER": "1000018220206",
    "tzyy": "e3e374bab2b1423085c666954b5f62eb",
    "x5check_ele": "NwxY7tHGHK3DQVrUEyJtlkhooQiq0w9aT63cDefnDh8%3D",
    "tfstk": "c5LGBu4R1hS_stsHVV_6wFc4TsQcZGaVEE8B8e-2erx1tK8FigrU42extOvMko1..",
    "l": "eBNHMeUVOopGhFTyBO5Z-urza77TPIdf1sPzaNbMiInca6TFdKyNuNQVtmwefdtjgtfAsetybUMLyRnD87zT5KslMJZWhSpT1W99-",
    "isg": "BNnZ5Lzb4seJ2b5SusqtSb3N6MWzZs0YIEUKz_uP-oA7AvqUQ7cB6ucQBMZ0umVQ",
    "x5sec": "7b227466653b32223a2238303361366135316435623136393835323033393238383930346362646463654349586776767746454a6a35784c2f5732394b2f76774561447a45774d4441774d5467794d6a41794d4459374f413d3d227d"

}


shopIdList = [] ##店铺id列表
failStr = "{\"message\":\"用户网络信息异常\",\"name\":\"NEED_SLIDE\"}"  ##账号被ban后获取的json
voidStr = "{\"has_next\":false,\"items\":[],\"meta\":{\"rank_id\":\"\"}}"  ##当前位置所有店铺信息获取完毕后获取到的空json


for i in range(0,1):

    latitudeMin = latiAndlongiList[i][0][0]
    latitudeMax = latiAndlongiList[i][1][0]
    longitudeMin = latiAndlongiList[i][0][1]
    longitudeMax = latiAndlongiList[i][1][1]

    latiStep = (latitudeMax - latitudeMin) / 5
    longiStep = (longitudeMax - longitudeMin) / 5
    ## 每个城市遍历5x5=25个经纬度

    while (latitudeMin < latitudeMax):
        while (longitudeMin < longitudeMax):

            url = functions.urlExtend(basicUrl, [latitudeMin, longitudeMin])
            print("Current position is " + str(latitudeMin) + " " + str(longitudeMin))
            print(url)

            for j in range(0, 500, 30):
                print("Searching page " + str(j // 30 + 1))
                tempurl = url + str(j) + basicLimit
                print(""+tempurl)
                data = requests.get(tempurl, headers=header)
                print(data)
                data.encoding = 'utf-8'
                content = data.text
                print(content)
                if content == failStr:
                    print("spider fail") ##账号被ban输出异常提示
                    break
                if content == voidStr: ##当前经纬度位置遍历完毕
                    break
                with open("./.txt".format(str(content[0:10])), "w", encoding='utf-8') as f:
                    f.write(content)
                    f.close()


            longitudeMin += longiStep
            longitudeMin = round(longitudeMin, 6) ##防止出现浮点数误差
        latitudeMin += latiStep
        latitudeMin = round(latitudeMin, 6)  ##防止出现浮点数误差
